/*
 * @Author: Zhang
 * @Date: 2020-05-17 12:15:59
 * @LastEditTime: 2020-10-31 17:55:43
 * @LastEditors: firesky
 */
import { login, logout, getInfo, FQXMenu } from '@/api/login'
import { getToken, setToken, removeToken, setUserName } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    GET_MENUSLIST(state, payload) {
      state.menusList = payload
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password.trim()
      const db = userInfo.db
      const ztnamedb = encodeURIComponent(userInfo.ztnamedb)
      const info = {
        username,
        password,
        db
      }
      return new Promise((resolve, reject) => {
        login(info).then(response => {
          if (Object.keys(response.Data).length > 0) {
            const _is_auth = response.Data._is_auth
            const data = {
              db,
              _is_auth,
              ztnamedb
            }

            setUserName(username)
            commit('SET_NAME', username)

            setToken(data)
            commit('SET_TOKEN', data)
            resolve(response)
          }
        }).catch(error => {
          console.log(error, 111)
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data
          data.roles = ['admin']
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_NAME', data.name)
          // commit('SET_AVATAR', data.avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          // commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    GETMENUSLIST({ commit }, passParam) {
      // console.log(passParam, ' passParam')
      return new Promise((resolve, reject) => {
        FQXMenu(passParam).then(response => {
          resolve(response)
        }).catch(error => {
          console.log(error, 222)
          reject(error)
        })
      })
    }

  }
}

export default user
